package {
	import fl.controls.Button;
	
	import com.googlecode.milib.MILibMain;
	import com.googlecode.milib.dbg.InstanceDebugServiceProxy;
	import com.googlecode.milib.dbg.MLDBG;
	import com.googlecode.milib.dbg.SimpleTrace;
	import com.googlecode.milib.service.singleton.KeyboardService;
	import com.googlecode.milib.vo.MLEvent;
	
	import flash.display.BitmapData;
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.MouseEvent;	

	/**
	 * Debugger is using mldbg.swf, so you must place this file where is your main swf.
	 * 
	 * @author Marek Brun 'minim'
	 */
	public class UsingDebuggerMain extends MovieClip {
		
		private var dbg:InstanceDebugServiceProxy;
		public var arr:Array;
		public var obj:Object;
		private var ks:KeyboardService;
		public var bd:BitmapData;
		public var objects:Array;
		private var someClass:SomeClass;

		public function UsingDebuggerMain() {
			
			//initializing of MILib library 
			MILibMain.init(this);
			
			MILibMain.getInstance().setupStandardStage();
			
			//If you want enable debugger please call MLDBG.init()
			//it will load "mldbg.swf" (debugger interface)			MLDBG.init();
			
			//debug service for "this" (every debug service have it's own debug window)
			dbg=InstanceDebugServiceProxy.forInstance(this);
			
			dbg.log('Hello World from '+dbg.link(this));
			dbg.log('Press keys T, A, V, P and O to see other debugger examples');
			dbg.log('some linked values');
			dbg.logv('some value count log');			dbg.logv('some value count log');			dbg.logv('some value count log');
			dbg.logv('some named value log', Math.random());
			
			bd=new BitmapData(100, 100);
			bd.noise(5);
			
			arr=['one', 2, true];
			obj={str:'one', num:2, bool:true, subObj:{}, bd:bd};
			dbg.log('linked Object:'+dbg.link(obj));
			dbg.log('linked Array:'+dbg.link(arr));			dbg.log('linked BitmapData:'+dbg.link(bd));
			
			ks=KeyboardService.getInstance();
			ks.addEventListener(ks.event_Down, onKeyboardServiceKeyDown);
			
			Button(MovieClip(this).btnCountValue).addEventListener(MouseEvent.CLICK, onBtnCountValueClick);
			Button(MovieClip(this).btnSetValue).addEventListener(MouseEvent.CLICK, onBtnSetValueClick);
			
			someClass=new SomeClass();
			
			//registering someClass to MovieClip boxes.red for ~+M access  
			InstanceDebugServiceProxy.forInstance(someClass).registerInDisplay(MovieClip(this).boxes.red);
			
			//registering "this" to MovieClip boxes2 for ~+M access  
			dbg.registerInDisplay(MovieClip(this).boxes2);
			
			dbg.openDBGWindow();
			
		}
		
		public function addSimpleTraceLog():void {
			SimpleTrace.trace('some simple trace log '+Math.random());
		}

		public function addMemoryTag():void {
			dbg.logMemoryTag('some memory tag '+Math.random());
		}
		
		public function addFPSTag():void {
			dbg.logFPSTag('some fps tag '+Math.random());
		}
		
//********************************************************************************************
//		events for SomeClass
//********************************************************************************************
		public function onKeyboardServiceKeyDown(event:MLEvent):void {
			switch(event.info.data.keyCode){
				case KeyboardService.KEY_T:
					dbg.log('abcdefghijklmno'.split('').join('\n'));
				break;
				case KeyboardService.KEY_A:
					dbg.log(String(Math.random()*99999999));
				break;
				case KeyboardService.KEY_V:
					dbg.logv('count');
					dbg.logv('randomNumber', Math.random()*99999999);
				break;
				case KeyboardService.KEY_P:
					dbg.logv('new'+int(Math.random()*100));
				break;
				case KeyboardService.KEY_O:
					dbg.openDBGWindow();
				break;
				case KeyboardService.KEY_B:
					bd.noise(Math.random()*300);
				break;
			}
		}
		
		protected function onBtnCountValueClick(event:Event):void {
			dbg.logv('justCount');
		}
		
		protected function onBtnSetValueClick(event:Event):void {
			dbg.logv('num', Math.random());
		}
		
	}
}
